package views.editingSupports;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;

import core.Property;

public class PropertyValueEditingSupport extends PropertyNameEditingSupport {
	private final TableViewer viewer;

	protected CellEditor getCellEditor(Object element) {
		if (((Property) element).getName().equals("Orientation")) {
			String[] gender = new String[4];
			gender[0] = "UP";
			gender[1] = "DOWN";
			gender[2] = "RIGHT";
			gender[3] = "LEFT";
			return new ComboBoxCellEditor(viewer.getTable(), gender, SWT.READ_ONLY);
		} else
			return new TextCellEditor(viewer.getTable());
	}

	public PropertyValueEditingSupport(TableViewer viewer) {
		super(viewer);
		this.viewer = viewer;
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected Object getValue(Object element) {
		if (((Property) element).getName().equals("Orientation")) {
			Property prop = (Property) element;
			if (prop.getValue().equalsIgnoreCase("UP")) {
				return 0;
			} else if (prop.getValue().equalsIgnoreCase("DOWN")) {
				return 1;
			} else if (prop.getValue().equalsIgnoreCase("RIGHT")) {
				return 2;
			} else {
				return 3;
			}
		}

		Property person = (Property) element;
		return person.getValue();
	}

	@Override
	protected void setValue(Object element, Object value) {
		if (((Property) element).getName().equals("Orientation")) {
			int val = (Integer) value;
			if (val == 0) {
				((Property) element).setValue("UP");
			} else if (val == 1) {
				((Property) element).setValue("DOWN");
			} else if (val == 2) {
				((Property) element).setValue("RIGHT");
			} else {
				((Property) element).setValue("LEFT");
			}
		} else
			((Property) element).setValue((String) value);
		viewer.refresh();
	}
}
